import { _decorator, Component, instantiate, Node, Prefab, math, log } from 'cc';
import { Bullet } from '../bullet/Bullet';
import { EnemyPlane } from '../plane/EnemyPlan';
import { Contant } from './Contant';
const { ccclass, property } = _decorator;

@ccclass('gameManger')
export class gameManger extends Component {

    // 飞机对象
    @property(Node)
    public airPlayer: Node = null  
    // 子弹对象
    @property(Prefab)
    public bullet01: Prefab = null
    @property(Prefab)
    public bullet02: Prefab = null 
    @property(Prefab)
    public bullet03: Prefab = null 
    @property(Prefab)
    public bullet04: Prefab = null 
    @property(Prefab)
    public bullet05: Prefab = null 

    // 敌机
    @property(Prefab)
    public enemy01: Prefab = null
    @property(Prefab)
    public enemy02: Prefab = null
    @property
    public enemy1Speed = 0.5;
    @property
    public enemy2Speed = 0.7;


    // 子弹管理节点
    @property(Node)
    public bulletManager: Node = null 
    // 射击的周期
    @property
    public shootTime = 0.3
    // 子弹移动的速度
    @property
    public bulletSpeed = 3
    // 当前射击的周期
    private _currShootTime = 0
    // 是否触摸
    private _isShooting = false;

    private _combinationInterval = Contant.Combination.PLAN1;

    /**
     * 页面刚进入的时候调用
     */
    start() {
        this._init()
    }

    _init(){
        // 开始就发射一颗
        this._currShootTime = this.bulletSpeed
        // 初始化敌机模式
        this.changePlanMode()
    }

    /**
     * 毫秒级调用，每毫秒调用一次
     * @param deltaTime 
     */
    update(deltaTime: number) {
        // 一次发射3颗
        this._currShootTime += deltaTime
        // 必须是触摸，同时不能在发射周期内
        if(this._isShooting && this._currShootTime > this.shootTime){
            this.createPlayerBullet()
            this._currShootTime = 0
        }
        console.log("_combinationInterval:",this._combinationInterval)
        // 根据敌机组合模式生成飞机
        // 生成一架飞机的模式
        if (this._combinationInterval === Contant.Combination.PLAN1) {
            this.createEnemyPlan()
            this._combinationInterval = 0
        } else if(this._combinationInterval === Contant.Combination.PLAN2) {
            
        }
    }

    /**
     * 创建子弹
     */
    createPlayerBullet(){
        console.log("开始创建子弹")
        // 因为是预制所以需要通过实例获取
        const bullet = instantiate(this.bullet01)
        // 需要获取父级节点
        bullet.setParent(this.bulletManager)
        // 设置子弹所在位置，对应飞机的位置
        const airPosition = this.airPlayer.getPosition();
        bullet.setPosition(airPosition.x, airPosition.y, airPosition.z - 9)
        const bulletComp = bullet.getComponent(Bullet)
        bulletComp.bulletSpeed = this.bulletSpeed
    }

    /**
     * 创建显示敌人飞机
     * @param value 
     */
    createEnemyPlan(){
        console.log("开始创建：")
        const whichEnemy = math.randomRangeInt(1,3);
        let prefab : Prefab = null
        let speed = 0
        if(whichEnemy === Contant.EnemyType.TYPE1){
            prefab = this.enemy01
            speed = this.enemy1Speed
        }else{
            prefab = this.enemy02
            speed = this.enemy2Speed
        }

        const enemy = instantiate(prefab)
        enemy.setParent(this.node)
        // 创建出敌机对象
        const enemyComp = enemy.getComponent(EnemyPlane)
        enemyComp.show(speed)

        // 飞机在X轴出现的位置
        const randomPos = math.randomRangeInt(-26, 25)
        // -50 表示飞机固定在z轴-50的位置出现
        enemy.setPosition(randomPos, 0, -50)

    }

    /**
     * 定时任务改变敌机类型
     */
    changePlanMode(){
        this.schedule(this._modelChanged, 1, 3);
    }
    _modelChanged(){
        console.log("_modelChanged",this._combinationInterval)
        this._combinationInterval++;
    }

    

    // 判断是否再触摸状态
    public isShoot(value: boolean){
        this._isShooting = value
    }



}


